#include <iostream>
#include <cstdio>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
#define ldb double
#define ll long long
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define nextLine() {int c = 0; while ((c = getchar()) != 10 && c != EOF); }
const ldb pi = fabsl(atan2l(0.0, -1.0));
const ldb eps = 1e-9;
const int inf = 1 << 28;
#define sqr(a) ((a)*(a))
#define log2 log22222

int n, m;
int a[102][102];
int c[102][102];
vector <pair <int ,int > > b;
const ldb log2 = logl(2.0);
ldb sigma;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};

void load()
{
	cin >> n >> m;
	b.resize(0);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			scanf("%d", &a[i][j]);
			c[i][j] = a[i][j];
			if (a[i][j])
				b.pb(mp(i, j));
		}
}

void count1(int i, int j)
{
	ldb r = 0.0;
	for (int k = 0; k < b.size(); k++)
	{
		r += (ldb)a[b[k].ff][b[k].ss] / b.size() * expl(-(((ldb)abs(i - b[k].ff) + abs(j - b[k].ss))) / sigma);
	}
	a[i][j] = (int)(r + 0.5);
			
}

void count2(int i, int j)
{
	ldb r = 0;
	ldb d = 0;
	for (int k = 0; k < b.size(); k++)
	{
		r += a[b[k].ff][b[k].ss] / (ldb)sqrtl(sqr(i - b[k].ff) + sqr(j - b[k].ss));
		d += 1. / ((ldb)sqrtl(sqr(i - b[k].ff) + sqr(j - b[k].ss)));
	}
	r /= d;
	a[i][j] = (int)(r + eps);
}

void count3(int i, int j)
{
	if (b.size() == 0) return;
	if (b.size() == 1) 
	{
		a[i][j] = a[b[0].ff][b[0].ss];
		return;
	}
	ldb r = 0;
	ldb d = 0;
	for (int k = 0; k < b.size(); k++)
	{
		r += a[b[k].ff][b[k].ss] / (ldb)expl(log2 * (max(abs(i - b[k].ff), abs(j - b[k].ss))));
		d += 1. / (ldb)expl(log2 * (max(abs(i - b[k].ff), abs(j - b[k].ss))));
	}
	r /= d;
//	r /= (b.size());
	a[i][j] = (int)(r + 0.5);
}

ldb countScore()
{
	ldb r = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (j + 1 < m)
				r += expl(log2 * (abs(a[i][j] - a[i][j + 1])));
			if (i + 1 < n)
				r += expl(log2 * (abs(a[i][j] - a[i + 1][j])));
		}
	}
	return logl(r) / log2;
}        
ll countScore2(int i, int j, int p)
{
	ll r = 0;
	for (int k = 0; k < 4; k++)
	{
		if (i + di[k] < 0 || i + di[k] >= n) continue;
		if (j + dj[k] < 0 || j + dj[k] >= m) continue;
		r -= 1ll << abs(a[i][j] - p - a[i + di[k]][j + dj[k]]);
		r += 1ll << abs(a[i][j] - a[i + di[k]][j + dj[k]]);
	}
	return r;
}

ll countScore3(int i, int j, int i1, int j1, int p)
{
	a[i][j]-= p;
	a[i1][j1]-= p;
	ldb r = 0;
	for (int k = 0; k < 4; k++)
	{
		if (i + di[k] < 0 || i + di[k] >= n) continue;
		if (j + dj[k] < 0 || j + dj[k] >= m) continue;
		r -= 1ll << abs(a[i][j] - a[i + di[k]][j + dj[k]]);
	}
	for (int k = 0; k < 4; k++)
	{
		if (i1 + di[k] < 0 || i1 + di[k] >= n) continue;
		if (j1 + dj[k] < 0 || j1 + dj[k] >= m) continue;
		if (i1 + di[k] == i && j1 + dj[k] == j) continue;
		r -= 1ll << abs(a[i1][j1] - a[i1 + di[k]][j1 + dj[k]]);
	}
	
	a[i][j]+=p;
	a[i1][j1]+=p;
	for (int k = 0; k < 4; k++)
	{
		if (i + di[k] < 0 || i + di[k] >= n) continue;
		if (j + dj[k] < 0 || j + dj[k] >= m) continue;
		r += 1ll << abs(a[i][j] - a[i + di[k]][j + dj[k]]);
	}
	for (int k = 0; k < 4; k++)
	{
		if (i1 + di[k] < 0 || i1 + di[k] >= n) continue;
		if (j1 + dj[k] < 0 || j1 + dj[k] >= m) continue;
		if (i1 + di[k] == i && j1 + dj[k] == j) continue;
		r += 1ll << abs(a[i1][j1] - a[i1 + di[k]][j1 + dj[k]]);
	}
	return r;
}

void solve()
{
//	sigma = b.size();
/*
	ldb l = 0;
	ldb r = 3000, q1, q2, m1, m2;
	for (int t = 0; t <= 100; t++)
	{
		cerr << l << " " << r << "\n";
		m1 = (l * 2 + r) / 3.0;
		m2 = (l + r * 2) / 3.0;
		sigma = m1;		
		for (int i = 0; i < n; i++)
			for (int j = 0 ; j < m ; j++)
			{
				if (c[i][j] > 0) continue;
				count1(i, j);
			}
		q1 = countScore();
		sigma = m2;		
		for (int i = 0; i < n; i++)
			for (int j = 0 ; j < m ; j++)
			{
				if (c[i][j] > 0) continue;
				count1(i, j);
			}
		q2 = countScore();
		if (q1 < q2 + eps)
			r = m2;
		else l = m1;
	}
	sigma = l;
	*/
	for (int i = 0; i < n; i++)
		for (int j = 0 ; j < m ; j++)
		{
			if (c[i][j] > 0) continue;
			count2(i, j);
		}
		/*
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}
	cout << "\n";
	*/
	

}


void solve2()
{	
	bool ok = true;
	ldb lst = countScore();
	for (int tt = 0; ok; tt++)
	{
//		cerr << lst << "\n";    
		ok = false;
		srand(time(0));
		for (int p = 0; p < 1000; p++)
		{	
			int i = rand() % n;
			int j = rand() % m;
			
				if (c[i][j] > 0) continue;
				for (int o = 0; !ok && o < 2; o++)
				{
					a[i][j] += o;
					if (countScore() < lst - eps)
						ok = true;
					for (int k = 0; !ok && k < 2; k++)
					{
						if (i + di[k] >= n || j + dj[k] >= m || c[i + di[k]][j + dj[k]] > 0) continue;
						for (int l = 0; l < 2; l++)
						{
							a[i + di[k]][j + dj[k]] += l;
							if (countScore() < lst - eps)
							{
								lst = countScore();
								ok = true;
							}
							else a[i + di[k]][j + dj[k]] -= l;
						}
					}
					if (!ok)
						a[i][j] -= o;
				}

		}
		lst = countScore();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (c[i][j]) cout << "[";
			cout << a[i][j];
			if (c[i][j]) cout << "]";
			cout << " ";
		}
		cout << "\n";
	}
	cout << "\n";
}

vector <pair <int, int > > q;
bool was[102][102];

void solve3()
{
	q.resize(0);
	memset(was,  0, sizeof was);
 	for (int i = 0; i < b.size(); i++)
 	{
 		q.push_back(b[i]);
 		was[b[i].ff][b[i].ss] = 1;
    }
    for (int i = 0; i < q.size(); i++)
    {
    	for (int k = 0; k < 4; k++)
    	{
    		if (q[i].ff + di[k] < 0 || q[i].ff + di[k] >= n || q[i].ss + dj[k] < 0 || q[i].ss + dj[k] >= m) continue;
    		if (was[q[i].ff + di[k]][q[i].ss + dj[k]]) continue;
    		was[q[i].ff + di[k]][q[i].ss+ dj[k]] = 1;
    		q.pb(mp(q[i].ff + di[k], q[i].ss + dj[k]));
    	}
    }
    bool ok = true;
   // ldb lst = countScore();
    for (int tt = 0; tt < 60 && ok; tt++)
    {
    	ok = false;
    	for (int i = 0; i < q.size(); i++)
    	{
    		if (c[q[i].ff][q[i].ss]) continue;
    		bool ok2 = true;
    		for (int p = 1; ok2; p++)
    		{
    		
    		ok2 = false;
    		a[q[i].ff][q[i].ss] += p;
    		if (countScore2(q[i].ff, q[i].ss, p) < 0)
    		{
    //			cerr << i << ":" << q[i].ff << " " << q[i].ss << " -- " << a[q[i].ff][q[i].ss] << "\n";
    			//lst = countScore();
    			ok = true;
    			ok2 = true;
    		}
    		else a[q[i].ff][q[i].ss] -= p;
    		
    		for (int k = 0; !ok && k < 4; k++)
    		{
    			if (q[i].ff + di[k] < 0 || q[i].ff + di[k] >= n
    			|| q[i].ss + dj[k] < 0 || q[i].ss + dj[k] >= m
    			|| c[q[i].ff + di[k]][q[i].ss + dj[k]]) continue;
    			a[q[i].ff][q[i].ss] += p;
    			a[q[i].ff + di[k]][q[i].ss + dj[k]] += p;
    			if (countScore3(q[i].ff, q[i].ss, q[i].ff + di[k], q[i].ss + dj[k], p) < 0)
    			{
    //				cerr << i << ":" << q[i].ff << " " << q[i].ss << " -- " << a[q[i].ff][q[i].ss] << "\n";
    				//lst = countScore();
    				ok = true;
    				ok2 = true;
    			}
    			else
    			{
    				a[q[i].ff][q[i].ss] -= p;
    				a[q[i].ff + di[k]][q[i].ss + dj[k]] -= p;
    			}
    		}

    		}
    		
    	}
    }
    for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
//			if (c[i][j]) cout << "[";
			printf("%d ", a[i][j]);
//			if (c[i][j]) cout << "]";
		}
		printf("\n");
	}
	printf("\n");
}

#define file "plain"

int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
  	#endif
  	int T;
  	cin >> T;
  	for (int i = 0; i < T; i++)
  	{
  		load();
 		solve();
  		solve3();
  	}
  	return 0;
}
